<?php

namespace app\admin\controller;

use app\common\business\WorkRob;
use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class Comments extends AdminBase
{

    /**
     * 评价列表
     * @return Json
     */
    public function getList(){
        list($type, $page, $limit, $keyword) = Param::postMore([
            ['type',0],
            ['page',1],
            ['limit',10],
            ['keyword',''],
        ], $this->request, true);

        $commentBis = new \app\common\business\Comments();
        $list = $commentBis->getListByType([],$type,$page,$limit,$keyword,$this->departmentId);
        return Show::success($list);
    }

    /**
     * 评价工人
     * @return Json
     */
    public function handlerComment(){
        list($work_id, $work_rob_id, $associated_id, $comments, $score) = Param::postMore([
            ['work_id',0],
            ['work_rob_id',0],
            ['associated_id',0],
            ['comments',''],
            ['score',0],
        ], $this->request, true);

        $data=[
            'work_id'=>$work_id,
            'work_rob_id'=>$work_rob_id,
            'type'=>1,
            'associated_id'=>$associated_id,
            'comments'=>$comments,
            'score'=>$score,
            'user_id'=>$this->departmentId
        ];
        if(empty($this->departmentId)){
            return Show::error([], '仅人力资源公司可评价');
        }

        $validate=new \app\admin\validate\Comments();
        if (!$validate->scene('commentUser')->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new \app\common\business\Comments())->handlerComment($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "评价成功");
        }
        return Show::error([], '评价失败');
    }

    /**
     * 获取评价信息
     * @return Json
     */
    public function getInfo(){
        list($work_id, $work_rob_id, $associated_id ) = Param::getMore([
            ['work_id',0],
            ['work_rob_id',0],
            ['associated_id',0],
        ], $this->request, true);

        $where=[
            'work_id'=>$work_id,
            'work_rob_id'=>$work_rob_id,
            'type'=>1,
            'associated_id'=>$associated_id,
            'user_id'=>$this->departmentId
        ];
        $info = (new \app\common\business\Comments())->getNormalByWhere($where);
        if (empty($info)) $info = null;
        return Show::success($info,'OK');
    }
}
